package cn.gson.crm.controller.pay;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.EmploDao;
import cn.gson.crm.model.dao.StudentDao;
import cn.gson.crm.model.dao.StudentPayDao;
import cn.gson.crm.model.dao.StudentPoDao;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.model.entity.Student;
import cn.gson.crm.model.po.FollowStudentPo;
import cn.gson.crm.model.po.StudentPayPo;

@WebServlet("/student/pay_detail")
public class StudentPayDetail extends BaseServlet {
	/**
	 * 学员缴费
	 */
	private static final long serialVersionUID = 1L;
	private StudentPayDao stuPayDao = new StudentPayDao();
	private StudentPoDao poDao = new StudentPoDao();

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String p = request.getParameter("page");
		String stuId = request.getParameter("stuId");
		String startDate = request.getParameter("startTime");
		String endDate = request.getParameter("endTime");
		String empName = request.getParameter("empName");
		Integer empId = null;
		if (empName != null && !empName.trim().isEmpty()) {
			Employee emp = new EmploDao().emploGetByUserName(empName);
			if (emp != null) {
				empId = emp.getId();
			}
		}
		if (p == null || p.isEmpty()) {
			p = "1";
		}
		Integer page = Integer.parseInt(p);
		FollowStudentPo student = poDao.getStudentInfo(stuId);
		StudentPayPo studentDataPo = stuPayDao.getPayRecord(stuId);
		List<StudentPayPo> studentPayList = null;
		Integer totalPage = null;
		//首次进入时startDate，endDate为空，默认为当天零点到当前
		if (startDate == null || endDate == null) {
			studentPayList = stuPayDao.getPayList(page, Integer.parseInt(stuId), empId, sdf.parse(getCurrentDayZero()),
					sdf.parse(sdf.format(new Date())));
			totalPage = stuPayDao.getPayTotalPage(Integer.parseInt(stuId), empId, sdf.parse(getCurrentDayZero()),
					sdf.parse(sdf.format(new Date())));
			request.setAttribute("end", sdf.format(new Date()));
			request.setAttribute("start", getCurrentDayZero());
		} else {
			//当startDate，endDate为空时，则表示只输入员工名字查询
			if (startDate.isEmpty() || endDate.isEmpty()) {
				studentPayList = stuPayDao.getPayList(page, Integer.parseInt(stuId), empId);
				totalPage = stuPayDao.getPayTotalPage(Integer.parseInt(stuId), empId);
			} else {
				//当输入了员工名字和起止时间查询
				if (empName != null && !empName.trim().isEmpty()) {
					//存在该员工收款记录
					if (empId != null) {
						studentPayList = stuPayDao.getPayList(page, Integer.parseInt(stuId), empId,
								sdf.parse(startDate), sdf.parse(endDate));
						totalPage = stuPayDao.getPayTotalPage(Integer.parseInt(stuId), empId, sdf.parse(startDate),
								sdf.parse(endDate));
					} 
					//当不存在该员工在该时间段内的收款记录时，就直接设置数据为空
					else {
						studentPayList = null;
						totalPage = 0;
					}
				} 
				//当只输入了起止时间查询
				else {
					studentPayList = stuPayDao.getPayList(page, Integer.parseInt(stuId), empId, sdf.parse(startDate),
							sdf.parse(endDate));
					totalPage = stuPayDao.getPayTotalPage(Integer.parseInt(stuId), empId, sdf.parse(startDate),
							sdf.parse(endDate));
				}

				request.setAttribute("start", startDate);
				request.setAttribute("end", endDate);
			}
		}
		request.setAttribute("page", page);
		request.setAttribute("studentPayList", studentPayList);
		request.setAttribute("po", studentDataPo);
		request.setAttribute("student", student);
		request.setAttribute("queryString", noPageQueryString(request));
		request.setAttribute("totalPage", totalPage);
		getServletPathAndForward(request, "pay/student_pay_detail.jsp");
	}

}
